﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XJay.Identity.Common.Helper.DataExpansion
{
    public static class TimeExpansion
    {
        private static ChineseLunisolarCalendar cncld = new ();

        /// <summary>
        /// 阳历转阴历
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime SolarToLunar(this DateTime time)
        {
            int year = cncld.GetYear(time);
            // 是否有闰月,返回正整数（2020年闰4月，返回值为5）
            int flag = cncld.GetLeapMonth(year);
            int month = flag > 0 ? cncld.GetMonth(time) - 1 : cncld.GetMonth(time);
            int day = cncld.GetDayOfMonth(time);
            return new DateTime(year,month,day);
        }

        /// <summary>
        /// 阴历转阳历
        /// </summary>
        /// <param name="time">2021-07-24</param>
        /// <returns>2021-08-31</returns>
        public static DateTime LunarToSolar(this DateTime time)
        {
            int year = cncld.GetYear(time);
            int month = cncld.GetMonth(time);
            int day = cncld.GetDayOfMonth(time);
            int flag = cncld.GetLeapMonth(year);
            DateTime dtnl = cncld.ToDateTime(year, month, day, 0, 0, 0, 0);
            return flag > 0 ? dtnl.AddMonths(1) : dtnl;
        }
    }
}
